<?php

require_once(WSB_XTENSION_PATH . 'simpleshop/models/DbTable/Article/Table.php');
require_once(WSB_XTENSION_PATH . 'simpleshop/models/DbTable/Basket/Table.php');

class Simpleshop_IndexController extends WSB_Xtension_Extension
{

	public function indexAction()
	{
		$config = $this->getConfiguration();
		$table = $this->getTable();
		$select = $table->select();
		$select->where('deleted=?', 0);
		$select->where('hidden=?', 0);
		$select->where('instance_id=?', $this->getModuleId());
		$this->_view->articles = $table->fetchAll($select);
		$this->_view->currency = $config->currency;
		$this->addContent($this->_view->render('index/index.phtml'));
	}

	public function detailAction()
	{
		$id = intval($this->getParam('id'));
		if(empty($id)) {
			$this->addContent('Error: Invalid parameters');
		} else {
			$config = $this->getConfiguration();
			$this->_view->currency = $config->currency;
			$this->_view->article = $this->getTable()->getArticle($id);
			$this->addContent($this->_view->render('index/detail.phtml'));
		}
	}

	public function basketAction()
	{
		$forward = $this->getParam('forward', 'index');
		$forwardAction = $forward . 'Action';
		$articleId = $this->getParam('id', 0);
		$article = $this->getTable()->getArticle($articleId);
		if($article instanceof Simpleshop_Model_DbTable_Article_Row) {
			$basket = new Simpleshop_Model_DbTable_Basket_Table();
			$basket->addArticle($article);

			// Wird zur Zeit nur benoetigt, um das Caching auszuschalten.
			$session = new Zend_Session_Namespace('Simpleshop_Xtension');
			$session->basket = true;

		} else {
			$this->addContent('Error: Invalid Article');
		}
		$this->$forwardAction();
	}

	public function getTable()
	{
		return new Simpleshop_Model_DbTable_Article_Table();
	}

}
